package com.doubao.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.doubao.config.AppConfig;
import com.doubao.open.api.Constants;
import com.doubao.open.api.DefaultRequest;
import com.doubao.open.api.DefaultResponse;
import com.doubao.open.client.DoubaoClient;
import com.doubao.open.client.env.DouBaoEnvEnum;
import com.doubao.open.client.exception.DouBaoOpenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.doubao.config.AppConfig.doubaoAppsecret;
import static com.doubao.config.AppConfig.privateKey;

/**
 * description:Dubbouitl
 * <p>
 * author:sult
 * time: 2018/11/3 14:57
 **/
public class CallDubboUtil {

    private static final Logger log = LoggerFactory.getLogger(CallDubboUtil.class);

    public static void call(final String servicePath,String param) throws Exception {

        DefaultRequest request = new DefaultRequest() {

            public String serviceName() {
                return servicePath;
            }

            public void checkBizParams() throws DouBaoOpenException {

            }
        };

        request.setContent(param);
        request.setSignType(Constants.MD5);
        request.setEncrypt(false);

        DoubaoClient client = new DoubaoClient( DouBaoEnvEnum.PRD, AppConfig.doubaoAppkey, doubaoAppsecret, privateKey);
        try {

            DefaultResponse response =  client.call(request);
            log.info("调用[{}]服务返回结果：{}",servicePath,JSON.toJSONString(response));
            if(response!=null && response.getCode()==200){
//                JSONObject ato=JSONObject.parseObject(response.getContent());
//                if(ato!=null){
//                    System.out.println("--------------"+ato.toString());
//                }
            }
        } catch (Exception e) {
            log.error("调用[{}]服务失败！错误：{}",servicePath,e);
            e.printStackTrace();
            throw e;
        }

    }


}
